﻿<%@ WebHandler Language="C#" Class="ContainerSplitReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 流水卡分批查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2022.4.20
 **/

public class ContainerSplitReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getSearchSqlString(JObject jObject)
    {
        string sql = @"
SELECT cci.mfgordername,cci.productname,sp.splitcontainername togetherflag,cci.fromworkcentername workcentername,
       es.fullname startPerson,ws.workflowstepname splitworkflowstepname,
       sp.qty splitqty,sp.createdate splitdate,cci.workflowstepname currentworkflowstepname,
       CASE WHEN cci.isinstore = 1 THEN '已入库' WHEN cci.isinstore <> 1 THEN '未入库' END status
FROM containersplitinfo sp
LEFT JOIN containercurrentinfo cci ON cci.containername = sp.splitcontainername 
LEFT JOIN container c ON c.containerid = cci.containerid
LEFT JOIN employee es ON es.employeeid = c.startemployeeid
LEFT JOIN workflowstep ws ON ws.workflowstepid = sp.workflowstepid --分批工序
WHERE sp.isuse = 0 AND sp.splitresult = 0 AND cci.splitfromid IS NOT NULL AND cci.containerstatus = 1 
";
        //分厂
        if (jObject["factoryid"] != null && jObject["factoryid"].ToString() != "")
        {
            sql += string.Format(" AND cci.originalfactoryid = '{0}'", jObject["factoryid"].ToString());
        }

        //工区
        if (jObject["workcenterid"] != null && jObject["workcenterid"].ToString() != "")
        {
            sql += string.Format(" AND cci.fromworkcenterid = '{0}'", jObject["workcenterid"].ToString());
        }

        //订单号
        if (jObject["mfgordername"] != null && jObject["mfgordername"].ToString() != "")
        {
            sql += string.Format(" AND cci.mfgordername = '{0}'", jObject["mfgordername"].ToString());
        }

        //零件图号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND cci.productname = '{0}'", jObject["productname"].ToString());
        }

        //批次号
        if (jObject["containername"] != null && jObject["containername"].ToString() != "")
        {
            sql += string.Format(" AND sp.splitcontainername = '{0}'", jObject["containername"].ToString());
        }

        //分卡时间
        if (jObject["containersplitdate"] != null && jObject["containersplitdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["containersplitdate"].ToString());
            if (arr.Count == 2)
            {
                sql += string.Format(" AND TRUNC(sp.createdate) BETWEEN TO_DATE('{0}','yyyy-mm-dd') and TO_DATE('{1}','yyyy-mm-dd')", arr[0].ToString(), arr[1].ToString());
            }
        }

        //状态
        if (jObject["status"] != null && jObject["status"].ToString() != "")
        {
            if (jObject["status"].ToString() == "未入库")
            {
                sql += " AND cci.isinstore <> 1 ";
            }
            else
            {
                sql += " AND cci.isinstore = 1 ";
            }
        }

        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        Result r = new Result();

        string sql = getSearchSqlString(jObject);

        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);

        DataTable dt = dto.DBTable;

        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }

        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getSearchSqlString(jObject);
        DataTable dtResult = dal.GetDataTable(sql);

        foreach (DataColumn col in dtResult.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }

        dtResult.Columns["mfgordername"].Caption = "订单号";
        dtResult.Columns["productname"].Caption = "零件图号";
        dtResult.Columns["togetherflag"].Caption = "批次号";
        dtResult.Columns["workcentername"].Caption = "主制工区";
        dtResult.Columns["startperson"].Caption = "开卡人";
        dtResult.Columns["splitworkflowstepname"].Caption = "分卡工序";
        dtResult.Columns["splitqty"].Caption = "分卡数量";
        dtResult.Columns["splitdate"].Caption = "分卡时间";
        dtResult.Columns["currentworkflowstepname"].Caption = "当前工序";
        dtResult.Columns["status"].Caption = "状态";

        ExportHelper.ExportExcel(dtResult, "流水卡分批查询报表");
    }
}